package de.barny.main;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Main extends Application {
	
	int initialWidth = 900;
	int initialHeight = 600;

	public static void main(String[] args) {
		launch(args);
	}

	@Override
	public void start(Stage mainStage) throws Exception {
		VBox root = new VBox(); // Create a new root vertical box
		Scene mainScene = new Scene(root); // from which a new Scene is created
		mainScene.getStylesheets().add("de/barny/main/style/frameStyle.css");

		// MENUBAR START
		root.getChildren().add(new Menubar().createMenu());
		// MENUBAR END

		//CONTENT START
		final ScrollPane scrollPane = new ScrollPane();
		scrollPane.setFitToWidth(true);
		scrollPane.setFitToHeight(true);
		scrollPane.setPrefWidth(initialWidth);
		scrollPane.setPrefHeight(initialHeight);
		
		StackPane contentPane = new StackPane();
		
		HBox workerTimeLine = new HBox();
		
		//WORKERLIST START
		final WorkerList workerList = new WorkerList();
		
		workerTimeLine.getChildren().add(workerList);
		//WORKERLIST END
		
		// TIMELINE START
		final TimeTable timetable = new TimeTable((int)workerList.getWidth());
		
		workerTimeLine.getChildren().add(timetable);
		contentPane.getChildren().add(workerTimeLine);
		// TIMELINE END
		
		//DRAGGABLE BARS START ---- TEMPORARILY
		Pane dragPane = new Pane();
		final DraggableBar dragBar = new DraggableBar(timetable.getHourCount());
		dragPane.getChildren().add(dragBar.getBar());
		dragPane.setTranslateX(130);
		dragPane.setTranslateY(70);
		contentPane.getChildren().add(dragPane);
		//DRAGGABLE BARS END
		
		scrollPane.setContent(contentPane);
		root.getChildren().add(scrollPane);
		//CONTENT END
		
		// WINDOW RESIZE CHANGELISTENER START
		mainScene.widthProperty().addListener(new ChangeListener<Number>() {

			@Override
			public void changed(
					ObservableValue<? extends Number> observableValue,
					Number oldSceneWidth, Number newSceneWidth) {
				timetable.setTimeTableWidth(newSceneWidth.intValue());
				dragBar.setHourLines(timetable.getLines());
				
			}
		});
		
		mainScene.heightProperty().addListener(new ChangeListener<Number>() {

			@Override
			public void changed(
					ObservableValue<? extends Number> observableValue,
					Number oldSceneHeight, Number newSceneHeight) {
				scrollPane.setPrefHeight(newSceneHeight.intValue());
				workerList.setBoxHeight((int) scrollPane.getHeight());
				timetable.setTimeTableHeight((int) scrollPane.getHeight());
				
				
			}
		});
		// WINDOW RESIZE CHANGELISTENER END
		
		mainStage.setScene(mainScene); // Set the main scene
		mainStage.setWidth(initialWidth);
		mainStage.setHeight(initialHeight);
		mainStage.setMinWidth(500);
		mainStage.setMinHeight(300);
		mainStage.setTitle("Personaleinsatz Manager"); // title the application
		mainStage.show(); // Make the application visible
	}

}
